//package cn.xspace.wnx.db;
//
//import android.content.Context;
//import android.os.Environment;
//import android.widget.Toast;
//
//import com.cn.wt.wtutils.DbUtils;
//import com.cn.wt.wtutils.db.sqlite.Selector;
//import com.cn.wt.wtutils.db.sqlite.WhereBuilder;
//import com.cn.wt.wtutils.exception.DbException;
//
//import java.util.ArrayList;
//import java.util.List;
//
//import com.xspace.android.xafcommon.appliction.BaseAppliction;
//import cn.xspace.wnx.data.INoticeData;
//import cn.xspace.wnx.entity.Body;
//import cn.xspace.wnx.entity.MessageEntity;
//import cn.xspace.wnx.entity.NoticeOrWNX;
//import cn.xspace.wnx.entity.NoticeOrWNXmain;
//import cn.xspace.wnx.entity.PushBean;
//import rx.Observable;
//import rx.Subscriber;
//
///**
// * Created by Administrator on 2016/10/17 0017.
// */
//public class NoticeOrWNXDao {
//    private Context context;
//    private static NoticeOrWNXDao instance;
//
//    public static NoticeOrWNXDao getInstance() {
//        if (instance == null) {
//            instance = new NoticeOrWNXDao();
//        }
//        return instance;
//    }
//
//    private NoticeOrWNXDao() {
//    }
//
//    public INoticeData getiNoticeData() {
//        return null;
//    }
//
////    private INoticeData iNoticeData = new INoticeData() {
////        @Override
////        public Observable<String> insertWnxOrNotice(final PushBean model, final List<Body.NewssnapBean> newssnap) {
////            return Observable.create(new Observable.OnSubscribe<String>() {
////                @Override
////                public void call(Subscriber<? super String> subscriber) {
////                    setAdd2(model, newssnap);
////                    subscriber.onNext("success");
////                }
////            });
////        }
////
////        @Override
////        public Observable<List<NoticeOrWNXmain>> getDBNoticeOrWNXInfoList(final String msg_type) {
////            return Observable.create(new Observable.OnSubscribe<List<NoticeOrWNXmain>>() {
////                @Override
////                public void call(Subscriber<? super List<NoticeOrWNXmain>> subscriber) {
////                    subscriber.onNext(getDBNoticeOrWNXInfoList2(msg_type));
////                }
////            });
////        }
////
////        @Override
////        public Observable<String> delInfo(final NoticeOrWNXmain delete) {
////            return Observable.create(new Observable.OnSubscribe<String>() {
////                @Override
////                public void call(Subscriber<? super String> subscriber) {
////                    delInfo2(delete);
////                    subscriber.onNext("success");
////                }
////            });
////        }
////
////        @Override
////        public Observable<List<NoticeOrWNX>> getDBInfoListbyMsgId(final String msgId) {
////            return Observable.create(new Observable.OnSubscribe<List<NoticeOrWNX>>() {
////                @Override
////                public void call(Subscriber<? super List<NoticeOrWNX>> subscriber) {
////                    subscriber.onNext(getDBInfoListbyMsgId2(msgId));
////                }
////            });
////        }
////
////        @Override
////        public Observable<List<NoticeOrWNX>> getDBInfoListbyMsgType(final String msgType) {
////            return Observable.create(new Observable.OnSubscribe<List<NoticeOrWNX>>() {
////                @Override
////                public void call(Subscriber<? super List<NoticeOrWNX>> subscriber) {
////                    subscriber.onNext(getDBInfoListbyMsgType2(msgType));
////                }
////            });
////        }
////
////        @Override
////        public Observable<String> updateDBInfobyMsgId(final String msgId) {
////            return Observable.create(new Observable.OnSubscribe<String>() {
////                @Override
////                public void call(Subscriber<? super String> subscriber) {
////                    updateDBInfobyMsgId2(msgId);
////                }
////            });
////        }
////
////        @Override
////        public Observable<String> updateInfo(final PushBean model, final MessageEntity messageEntity, final List<Body.NewssnapBean> newssnap) {
////            return Observable.create(new Observable.OnSubscribe<String>() {
////                @Override
////                public void call(Subscriber<? super String> subscriber) {
////                    updateInfo2(model, messageEntity, newssnap);
////                }
////            });
////        }
////    };
//
//
//    /**
//     * 从XMPP接收到的推送存放到数据库中
//     *
//     * @param model
//     * @param messageEntity
//     * @param newssnap
//     */
//    public void setAdd2(PushBean model, List<Body.NewssnapBean> newssnap) {
//        cn.xspace.wnx.util.LogUtils.e("进入setAdd了");
////        FileUtils.createDir(Environment.getExternalStorageDirectory() + "data/data/cn.xspace.wnx/databases/");
//        DbUtils dbUtils = DbUtils.create(BaseAppliction.mInstance, "qsfy_wnx.db", true);
//        NoticeOrWNX entity = new NoticeOrWNX();
////        entity.setUserId(i);
//        entity.setImg(newssnap.get(0).getImg());
//        entity.setSrc(newssnap.get(0).getSrc());
//        entity.setSummary(newssnap.get(0).getSummary());
//        entity.setTitle(newssnap.get(0).getTitle());
//        entity.setPlatForm(model.msgInfo.getPlatForm());
//        entity.setBizCode(model.msgInfo.getBizCode());
//        entity.setLogo(model.msgInfo.getLogo());
//        entity.setSenderId(model.msgInfo.getSenderId());
//        entity.setSenderName(model.msgInfo.getSenderName());
//        entity.setSenderPhone(model.msgInfo.getSenderPhone());
//        entity.setSendTime(model.msgInfo.getSendTime());
//        entity.setSendDeptId(model.msgInfo.getSendDeptId());
//        entity.setSendDeptName(model.msgInfo.getSendDeptName());
//        entity.setMsgId(model.msgInfo.getMsgId());
//        entity.setMsgLogo(model.msgInfo.getMsgLogo());
//        entity.setMsgSequenceId(model.msgInfo.getMsgSequenceId());
//        entity.setMsgType(model.msgInfo.getMsgType());
//        entity.setMsgChannel(model.msgInfo.getMsgChannel());
//        entity.setInserttime(model.msgInfo.getSendTime());
////       entity.setInserttime( System.currentTimeMillis()+"");
//        entity.setMsg_warn_status(0);
//
//
//        try {
//            dbUtils.save(entity);
//            List<NoticeOrWNX> orWNXes = dbUtils.findAll(Selector.from(NoticeOrWNX.class));
////            for (int i = 0; i < orWNXes.size(); i++) {
////                com.cn.wt.wtutils.log.LogUtils.e("一开始从XMPP接收到存起来后看看数据库数据:" + orWNXes.get(i));
////            }
//
//        } catch (DbException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 通过消息类型查询或得NoticeOrWNXmain的集合
//     *
//     * @param msg_type 消息类型
//     * @return
//     */
//    public List<NoticeOrWNXmain> getDBNoticeOrWNXInfoList2(String msg_type) {
////        FileUtils.createDir(Environment.getExternalStorageDirectory() + "/test/");
//        DbUtils dbUtils = DbUtils.create(BaseAppliction.mInstance, "qsfy_wnx.db", true);
//        int count = 0;
//        int unCount = 0;
//        List<NoticeOrWNXmain> noticeOrWNXmainList = new ArrayList<>();//未读在前已读在后的list
//        try {
//            if (msg_type.equalsIgnoreCase("0")) {
////                com.cn.wt.wtutils.log.LogUtils.e("开始取通知公告的信息了:");
//                List<NoticeOrWNX> noticeOrWNX = dbUtils.findAll(Selector.from(NoticeOrWNX.class).where(WhereBuilder.b("bizCode", "=", "01")));
////                for (int i = 0; i < noticeOrWNX.size(); i++) {
////                    com.cn.wt.wtutils.log.LogUtils.e("看看数据库通追公告数据:" + noticeOrWNX.get(i));
////                }
//
//                for (int i = noticeOrWNX.size() - 1; i >= 0; i--) {
//                    if (noticeOrWNX.get(i).isRead == false) {
//                        NoticeOrWNXmain OrWNX = new NoticeOrWNXmain();
//                        OrWNX.setUserId(noticeOrWNX.get(i).getUserId());
//                        OrWNX.setImg(noticeOrWNX.get(i).getImg());
//                        OrWNX.setSrc(noticeOrWNX.get(i).getSrc());
//                        OrWNX.setPlatForm(noticeOrWNX.get(i).getPlatForm());
//                        OrWNX.setBizCode(noticeOrWNX.get(i).getBizCode());
//                        OrWNX.setMsgLogo(noticeOrWNX.get(i).getMsgLogo());
//                        OrWNX.setLogo(noticeOrWNX.get(i).getLogo());
//                        OrWNX.setSendDeptId(noticeOrWNX.get(i).getSendDeptId());
//                        OrWNX.setSendDeptName(noticeOrWNX.get(i).getSendDeptName());
//                        OrWNX.setSummary(noticeOrWNX.get(i).getSummary());
//                        OrWNX.setSendTime(noticeOrWNX.get(i).getSendTime());
//                        OrWNX.setInserttime(noticeOrWNX.get(i).getInserttime());
//                        OrWNX.setRead(noticeOrWNX.get(i).isRead());
//                        OrWNX.setMsgId(noticeOrWNX.get(i).getMsgId());
//                        OrWNX.setTitle(noticeOrWNX.get(i).getTitle());
//                        OrWNX.setSenderId(noticeOrWNX.get(i).getSenderId());
//                        OrWNX.setSenderName(noticeOrWNX.get(i).getSenderName());
//                        OrWNX.setSenderPhone(noticeOrWNX.get(i).getSenderPhone());
//                        OrWNX.setMsgSequenceId(noticeOrWNX.get(i).getMsgSequenceId());
//                        OrWNX.setMsgType(noticeOrWNX.get(i).getMsgType());
//                        OrWNX.setMsgChannel(noticeOrWNX.get(i).getMsgChannel());
//                        OrWNX.setMsg_warn_status(noticeOrWNX.get(i).getMsg_warn_status());
//                        noticeOrWNXmainList.add(OrWNX);
//                    }
//                }
//
//                for (int i = noticeOrWNX.size() - 1; i >= 0; i--) {
//                    if (noticeOrWNX.get(i).isRead) {
//                        NoticeOrWNXmain OrWNX = new NoticeOrWNXmain();
//                        OrWNX.setUserId(noticeOrWNX.get(i).getUserId());
//                        OrWNX.setImg(noticeOrWNX.get(i).getImg());
//                        OrWNX.setSrc(noticeOrWNX.get(i).getSrc());
//                        OrWNX.setPlatForm(noticeOrWNX.get(i).getPlatForm());
//                        OrWNX.setBizCode(noticeOrWNX.get(i).getBizCode());
//                        OrWNX.setMsgLogo(noticeOrWNX.get(i).getMsgLogo());
//                        OrWNX.setLogo(noticeOrWNX.get(i).getLogo());
//                        OrWNX.setSendDeptId(noticeOrWNX.get(i).getSendDeptId());
//                        OrWNX.setSendDeptName(noticeOrWNX.get(i).getSendDeptName());
//                        OrWNX.setSummary(noticeOrWNX.get(i).getSummary());
//                        OrWNX.setSendTime(noticeOrWNX.get(i).getSendTime());
//                        OrWNX.setInserttime(noticeOrWNX.get(i).getInserttime());
//                        OrWNX.setRead(noticeOrWNX.get(i).isRead());
//                        OrWNX.setMsgId(noticeOrWNX.get(i).getMsgId());
//                        OrWNX.setTitle(noticeOrWNX.get(i).getTitle());
//                        OrWNX.setSenderId(noticeOrWNX.get(i).getSenderId());
//                        OrWNX.setSenderName(noticeOrWNX.get(i).getSenderName());
//                        OrWNX.setSenderPhone(noticeOrWNX.get(i).getSenderPhone());
//                        OrWNX.setMsgSequenceId(noticeOrWNX.get(i).getMsgSequenceId());
//                        OrWNX.setMsgType(noticeOrWNX.get(i).getMsgType());
//                        OrWNX.setMsgChannel(noticeOrWNX.get(i).getMsgChannel());
//                        OrWNX.setMsg_warn_status(noticeOrWNX.get(i).getMsg_warn_status());
//                        noticeOrWNXmainList.add(OrWNX);
//                    }
//                }
////                for (int i = 0; i < noticeOrWNXmainList.size(); i++) {
////                    com.cn.wt.wtutils.log.LogUtils.e("看看加入到noticeOrWNXmainList的数据:" + noticeOrWNX.get(i));
////                }
//
//            } else {
//                List<NoticeOrWNX> noticeOrWNX = dbUtils.findAll(Selector.from(NoticeOrWNX.class).where(WhereBuilder.b("bizCode", "=", "02")));
////                for (int i = 0; i < noticeOrWNX.size(); i++) {
////                    com.cn.wt.wtutils.log.LogUtils.e("看看数据库weinongxin数据:" + noticeOrWNX.get(i));
////                }
//                List<String> SendDeptIdList = new ArrayList<>();//SendDeptId的集合
//                for (int i = 0; i < noticeOrWNX.size(); i++) {
//                    if (i == 0) {
//                        SendDeptIdList.add(noticeOrWNX.get(i).getSendDeptId());
//                    } else {
//                        for (int j = i + 1; j < SendDeptIdList.size(); j++) {
//                            if (!noticeOrWNX.get(i).getSendDeptId().equalsIgnoreCase(SendDeptIdList.get(j))) {
////                                LogUtils.e("SendDeptId：" + noticeOrWNX.get(i).getSendDeptId());
//                                SendDeptIdList.add(noticeOrWNX.get(i).getSendDeptId());
//                            }
//                        }
//                    }
//
//                }
////                LogUtils.e("所有的机构ID：" + SendDeptIdList);
//                for (int x = 0; x < SendDeptIdList.size(); x++) {
//                    String msgLogo = null, logo = null, sendDeptName = null, inserttime = null, summary = null, sendTime = null;
//                    Boolean isRead;
//                    for (int y = 0; y < noticeOrWNX.size(); y++) {
//                        if (SendDeptIdList.get(x).equalsIgnoreCase(noticeOrWNX.get(y).getSendDeptId())) {
//                            count++;
//                            msgLogo = noticeOrWNX.get(y).getMsgLogo();
//                            logo = noticeOrWNX.get(y).getLogo();
//                            sendDeptName = noticeOrWNX.get(y).getSendDeptName();
//                            summary = noticeOrWNX.get(y).getSummary();
//                            sendTime = noticeOrWNX.get(y).getSendTime();
//                            inserttime = noticeOrWNX.get(y).getInserttime();
//                            if ("false".equalsIgnoreCase(noticeOrWNX.get(y).isRead() + "")) {
//                                unCount++;
//                            }
//                        }
//                    }
//                    //下面这段代码有待改进
//                    NoticeOrWNXmain noticeOrWNXmain = new NoticeOrWNXmain();
//                    noticeOrWNXmain.setMsgLogo(msgLogo);
//                    noticeOrWNXmain.setLogo(logo);
//                    noticeOrWNXmain.setNum(count);
//                    noticeOrWNXmain.setSendDeptId(SendDeptIdList.get(x));
//                    noticeOrWNXmain.setSendDeptName(sendDeptName);
//                    noticeOrWNXmain.setSummary(summary);
//                    noticeOrWNXmain.setSendTime(sendTime);
//                    noticeOrWNXmain.setInserttime(inserttime);
//                    noticeOrWNXmain.setUnCount(unCount);
////                    LogUtils.e("看看数据count：" + count);
////                    LogUtils.e("msgLogo：" + msgLogo);
////                    LogUtils.e("SendDeptId：" + SendDeptIdList.get(x));
////                    LogUtils.e("SendDeptName：" + sendDeptName);
////                    LogUtils.e("Summary：" + summary);
////                    LogUtils.e("sendTime：" + sendTime);
////                    LogUtils.e("inserttime：" + inserttime);
////                    LogUtils.e("未读消息数量：" + unCount);
//                    noticeOrWNXmainList.add(noticeOrWNXmain);
//                }
//            }
//        } catch (DbException e) {
//        }
//        return noticeOrWNXmainList;
//    }
//
//
//    /**
//     * 根据UserId从数据可删除消息
//     */
//    public void delInfo2(NoticeOrWNXmain delete) {
////        FileUtils.createDir(Environment.getExternalStorageDirectory() + "/test/");
//        DbUtils dbUtils = DbUtils.create(BaseAppliction.mInstance, "qsfy_wnx.db", true);
//        try {
//            List<NoticeOrWNX> noticeOrWNX = dbUtils.findAll(Selector.from(NoticeOrWNX.class));
//            dbUtils.deleteById(NoticeOrWNX.class, delete.getUserId());
//        } catch (DbException e) {
//            // TODO Auto-generated catch block
//            Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
//        }
//    }
//
//    /**
//     * 通过msgId查询NoticeOrWNX的集合
//     *
//     * @param msgId
//     * @return
//     */
//    public List<NoticeOrWNX> getDBInfoListbyMsgId2(String msgId) {
////        FileUtils.createDir(Environment.getExternalStorageDirectory() + "/test/");
//        DbUtils dbUtils = DbUtils.create(BaseAppliction.mInstance, "qsfy_wnx.db", true);
//        List<NoticeOrWNX> noticeOrWNX = new ArrayList<>();
//        try {
//            noticeOrWNX = dbUtils.findAll(Selector.from(NoticeOrWNX.class).where(WhereBuilder.b("msgId", "=", msgId)));
////            LogUtils.e("这是根据sql语句查找的一条结果" + noticeOrWNX.get(0).toString());
////            DbModel findDbModelFirst=dbUtils.findDbModelFirst("select * from NoticeOrWNX");
////            LogUtils.e("这是根据sql语句查找的一条结果"+findDbModelFirst.getString(msgId));
//        } catch (DbException e) {
//            e.printStackTrace();
//        }
//        return noticeOrWNX;
//    }
//
//    /**
//     * 通过msgType查询NoticeOrWNX的集合
//     *
//     * @param msgType
//     * @return
//     */
//    public List<NoticeOrWNX> getDBInfoListbyMsgType2(String msgType) {
////        FileUtils.createDir(Environment.getExternalStorageDirectory() + "/test/");
//        DbUtils dbUtils = DbUtils.create(BaseAppliction.mInstance, "qsfy_wnx.db", true);
//        List<NoticeOrWNX> noticeOrWNX = new ArrayList<>();
//        try {
//            if ("全部".equalsIgnoreCase(msgType)) {
//                noticeOrWNX = dbUtils.findAll(Selector.from(NoticeOrWNX.class));
//            } else {
//                noticeOrWNX = dbUtils.findAll(Selector.from(NoticeOrWNX.class).where(WhereBuilder.b("msgType", "=", msgType)));
//            }
//
////            LogUtils.e("这是根据sql语句查找的一条结果" + noticeOrWNX.get(0).toString());
////            DbModel findDbModelFirst=dbUtils.findDbModelFirst("select * from NoticeOrWNX");
////            LogUtils.e("这是根据sql语句查找的一条结果"+findDbModelFirst.getString(msgId));
//        } catch (DbException e) {
//            e.printStackTrace();
//        }
//        return noticeOrWNX;
//    }
//
//    /**
//     * 根据msgId升级某一条数据
//     *
//     * @param msgId
//     */
//    public void updateDBInfobyMsgId2(String msgId) {
////        FileUtils.createDir(Environment.getExternalStorageDirectory() + "/test/");
//        DbUtils dbUtils = DbUtils.create(BaseAppliction.mInstance, "qsfy_wnx.db", true);
//        try {
//            List<NoticeOrWNX> noticeOrWNX = dbUtils.findAll(Selector.from(NoticeOrWNX.class).where(WhereBuilder.b("msgId", "=", msgId)));
//            NoticeOrWNX orWNX = new NoticeOrWNX();
//            orWNX.setImg(noticeOrWNX.get(0).getImg());
//            orWNX.setSrc(noticeOrWNX.get(0).getSrc());
//            orWNX.setSummary(noticeOrWNX.get(0).getSummary());
//            orWNX.setTitle(noticeOrWNX.get(0).getTitle());
//            orWNX.setBizCode(noticeOrWNX.get(0).getBizCode());
//            orWNX.setLogo(noticeOrWNX.get(0).getLogo());
//            orWNX.setSenderId(noticeOrWNX.get(0).getSenderId());
//            orWNX.setSenderName(noticeOrWNX.get(0).getSenderName());
//            orWNX.setSenderPhone(noticeOrWNX.get(0).getSenderPhone());
//            orWNX.setSendTime(noticeOrWNX.get(0).getSendTime());
//            orWNX.setSendDeptId(noticeOrWNX.get(0).getSendDeptId());
//            orWNX.setSendDeptName(noticeOrWNX.get(0).getSendDeptName());
//            orWNX.setMsgId(noticeOrWNX.get(0).getMsgId());
//            orWNX.setMsgLogo(noticeOrWNX.get(0).getMsgLogo());
//            orWNX.setMsgSequenceId(noticeOrWNX.get(0).getMsgSequenceId());
//            orWNX.setMsgType(noticeOrWNX.get(0).getMsgType());
//            orWNX.setMsgChannel(noticeOrWNX.get(0).getMsgChannel());
//            orWNX.setInserttime(noticeOrWNX.get(0).getInserttime());
//            orWNX.setMsg_warn_status(noticeOrWNX.get(0).getMsg_warn_status());
//            orWNX.setRead(true);
////            dbUtils.update(orWNX, WhereBuilder.b("userId", "=", noticeOrWNX.get(noticeOrWNX.size()-1).getUserId()));
//            dbUtils.update(orWNX, WhereBuilder.b("userId", "=", noticeOrWNX.get(0).getUserId()));
////            LogUtils.e("更改后的数据是" + dbUtils.findAll(Selector.from(NoticeOrWNX.class).where(WhereBuilder.b("msgId", "=", msgId))).get(0).toString());
////            DbModel findDbModelFirst=dbUtils.findDbModelFirst("select * from NoticeOrWNX");
////            LogUtils.e("这是根据sql语句查找的一条结果"+findDbModelFirst.getString(msgId));
//        } catch (DbException e) {
//            e.printStackTrace();
//        }
//    }
//
//    private void updateInfo2(PushBean model, MessageEntity messageEntity, List<Body.NewssnapBean> newssnap) {
//        DbUtils dbUtils = DbUtils.create(context, Environment.getExternalStorageDirectory() + "/test/test.db", true);
//        try {
//            List<NoticeOrWNX> users = dbUtils.findAll(Selector.from(NoticeOrWNX.class));
//            NoticeOrWNX entity = new NoticeOrWNX();
//            entity.setUserId(0);
//            entity.setImg(newssnap.get(0).getImg());
//            entity.setSrc(newssnap.get(0).getSrc());
//            entity.setSummary(newssnap.get(0).getSummary());
//            entity.setTitle(newssnap.get(0).getTitle());
//            entity.setBizCode(model.msgInfo.getBizCode());
//            entity.setSenderId(model.msgInfo.getSenderId());
//            entity.setSenderName(model.msgInfo.getSenderName());
//            entity.setSenderPhone(model.msgInfo.getSenderPhone());
//            entity.setSendTime(model.msgInfo.getSendTime());
//            entity.setSendDeptId(model.msgInfo.getSendDeptId());
//            entity.setSendDeptName(model.msgInfo.getSendDeptName());
//            entity.setMsgId(model.msgInfo.getMsgId());
//            entity.setMsgLogo(model.msgInfo.getMsgLogo());
//            entity.setMsgSequenceId(model.msgInfo.getMsgSequenceId());
//            entity.setMsgChannel(model.msgInfo.getMsgChannel());
//            dbUtils.update(entity, WhereBuilder.b("userId", "=", users.get(users.size() - 1).getUserId()));
//        } catch (DbException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     *
//     */
//    private void delInfo2() {
//        DbUtils dbUtils = DbUtils.create(context, Environment.getExternalStorageDirectory() + "/test/test.db", true);
//        try {
//            List<NoticeOrWNX> noticeOrWNX = dbUtils.findAll(Selector.from(NoticeOrWNX.class));
//            dbUtils.deleteById(NoticeOrWNX.class, noticeOrWNX.get(noticeOrWNX.size() - 1).getUserId());
//        } catch (DbException e) {
//            // TODO Auto-generated catch block
//            Toast.makeText(context, "删除失败", Toast.LENGTH_SHORT).show();
//        }
//    }
//}
